package com.wswy.wyjk.ui.main.choose

import android.app.Activity
import android.arch.lifecycle.Observer
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.raft.framework.base.RFBaseFragment
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration
import com.wswy.wyjk.R
import com.wswy.wyjk.model.ICity
import com.wswy.wyjk.ui.EXTRA_DATA
import com.wswy.wyjk.ui.main.choose.model.CityEvent
import com.wswy.wyjk.ui.widget.SideBar
import kotlinx.android.synthetic.main.fragment_choose_city.*
import org.greenrobot.eventbus.EventBus
import java.util.*

class ChooseCityFragment : RFBaseFragment() {

    companion object {
        fun newInstance():RFBaseFragment{
            return ChooseCityFragment()
        }
    }

    private val indexed = IntArray(27) {-1}

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_choose_city
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val cityLoadViewModel = provideViewModel(CityLoadViewModel::class.java)

        val adapter = ChooseCityAdapter()
        val layoutManager = LinearLayoutManager(requireContext())

        recyclerView.layoutManager = layoutManager


        adapter.setOnItemClickListener { _, _, city ->
            EventBus.getDefault().post(CityEvent(city!!.cityName))
            activity?.setResult(Activity.RESULT_OK, Intent().apply {
                putExtra(EXTRA_DATA,city)
            })
            finish()
        }

        adapter.topHotClickListener = {
            EventBus.getDefault().post(CityEvent(it!!.cityName))
            activity?.
            setResult(Activity.RESULT_OK, Intent().apply {

                putExtra(EXTRA_DATA,it)
            })
            finish()
        }

        recyclerView.adapter = adapter

        recyclerView.addItemDecoration(StickyRecyclerHeadersDecoration(adapter))

        indexBar.setOnChooseLetterChangedListener(object : SideBar.OnChooseLetterChangedListener{
            override fun onChooseLetter(s: String?) {

                s?.get(0)?.run {

                    if (Character.isLetter(this)){

                        val pos = indexed[this - 'A']
                        if (pos >= 0) {
                            layoutManager.scrollToPositionWithOffset(pos, 0)
                        }

                    }else if (this == '#'){
                        layoutManager.scrollToPositionWithOffset(0, 0)
                    }

                }



            }

            override fun onNoChooseLetter() {
            }

        })


        cityLoadViewModel.getWZCityLiveData().observe(this, Observer {


            it?.run {

                var len = 0
                TreeMap<String, List<ICity>>(this).apply {
                    put("@", arrayListOf(ICity.HOT_PLACEHOLDER))
                }.flatMapTo(mutableListOf()) {
                    indexed[it.key[0] - '@'] = len
                    len += it.value.size
                    it.value
                }.run {

                    adapter.setNewData(this)
                    adapter.notifyDataSetChanged()
                }

            }

        })
        cityLoadViewModel.loadWZCity()
    }

    override fun getPageTitle(): String? {
        return "选择城市"
    }



}